package generation.aggregation;

import generation.duration.CellSpeed;
import generation.duration.DurationCellRepository;
import generation.duration.QuantifiedCell;
import generation.parts.Part;
import generation.scale.Scale;
import generation.scale.ScaleFactory;
import generation.transition.Transition;
import generation.transition.TransitionFactory;

import java.util.Date;

import mapping.Mapper;

import org.jfugue.IntervalNotation;
import org.jfugue.Pattern;

import util.ConfigFile;

import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;
import communication.OSCExternalInput;
import communication.SimpleXYMessage;

public class Aggregator implements OSCListener{
	
	private Scale scale;
	private Pattern pattern;

	public Aggregator(){
		this.scale = ScaleFactory.getScale("pentmaj");
		this.pattern = new Pattern();
	}
	
	public Pattern getPattern(){
		return this.pattern;
	}
	
	@Override
		public void acceptMessage(Date arg0, OSCMessage message) {
			Object[] args = message.getArguments();
			System.out.println("X:" + args[0] + " Y:" + args[1]);
			SimpleXYMessage normXY = new SimpleXYMessage((Float)args[0], (Float)args[1]);
			// Melody related code
			CellSpeed melodySpeed = Mapper.mapInputToSpeed(normXY.getX());
			QuantifiedCell melodyCell = DurationCellRepository.getRandomCellBySpeed(melodySpeed);
			System.out.println("Melody Cell: " + melodyCell);
			Transition melodyTransition = TransitionFactory.getMelodyTransition();
			String melodyPart = Part.generatePart(scale, melodyCell, melodyTransition);
			// Harmony related code
			CellSpeed harmonySpeed = Mapper.mapInputToSpeed(normXY.getY());
			QuantifiedCell harmonyCell = DurationCellRepository.getRandomCellBySpeed(harmonySpeed);
			Transition harmonyTransition = TransitionFactory.getHarmonyTransition();
			String harmonyPart = Part.generatePart(scale, harmonyCell, harmonyTransition);
			// Burocracy
			harmonyPart = "V0 " + ConfigFile.getString("instrument.chord") + " " + harmonyPart;
			melodyPart = "V1 " + ConfigFile.getString("instrument.melody") + " " + melodyPart;
			String song = harmonyPart + " " + melodyPart;
			//String song = melodyPart;
			IntervalNotation in = new IntervalNotation(song);
			Pattern tempPattern = in.getPatternForRootNote("C5");
			System.out.println("Adding new pattern: " + pattern.getMusicString());
			//pattern.add(tempPattern);
			PatternInformer.setInformation(tempPattern);
		}

	public static void main(String[] args) {
		Aggregator agg = new Aggregator();
		OSCExternalInput osc = new OSCExternalInput();
		osc.startListening(agg);
		TempoMarker tempo = new TempoMarker();
		tempo.start();
		
		//Player player = new Player();
		//StreamingPlayer splayer = new StreamingPlayer();
		//PatternListenerImpl listener = new PatternListenerImpl(splayer);
		//Pattern pattern = agg.getPattern();
		//pattern.addPatternListener(listener);
//		Player player = new Player();
//		Pattern pattern = new Pattern("V0 I4 [64]minw  V1 I4 [69]w  V0 I4 [64]mini [62]mini  V1 I4 [64]i [60]i  V0 I4 [65]majs [62]mins [69]mins [67]majs  V1 I4 [62]s [69]s [60]s [60]s  V0 I4 [65]majs [67]majs [60]majs [67]majs  V1 I4 [69]s [64]s [62]s [67]s  V0 I4 [62]mins [69]mins [69]mins [69]mins  V1 I4 [67]s [60]s [62]s [64]s  V0 I4 [65]majs [67]majs [69]mins [60]majs  V1 I4 [60]s [62]s [64]s [60]s  V0 I4 [62]mins [67]majs [65]maji  V1 I4 [60]s [67]s [60]s [69]s  V0 I4 [64]mini [64]mins [60]majs  V1 I4 [67]s [69]s [64]s [62]s ");
//		player.play(pattern);
//		try {
//			Thread.sleep(10000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		//player.play(agg.getPattern());
		//agg.startOSCReceiver();
	}

}
